﻿using System;
using System.ComponentModel;
using System.Windows.Media;
using CI.Misc;
using CI.Organism;

namespace CI.MSRoboticsEnv
{
    public class OrganismDrawModel : DataModel, IDisposable
    {
        public IOrganism Organism { get; private set; }

        private readonly UIConainerSize _uiContainer;
        private readonly double _xViewBoundry;
        private readonly double _yViewBoundry;
        private const double EdgeSpace = 10;

        public double StrokeThickness
        {
            get
            {
                return (Organism.Location.XRadius * 0.75) *
                    (_uiContainer.ContainerWidth / Organism.HostingEnvironment.EnvironmentScenario.BoundryXRadiusMax);
            }
        }

        private static readonly PropertyChangedEventArgs BrushColourPropetyChanged = new PropertyChangedEventArgs("BrushColour");
        private Brush _brushColour;
        public Brush BrushColour
        {
            get { return _brushColour; }
            set
            {
                if (_brushColour == value)
                    return;

                _brushColour = value;
                RaisePropertyChanged(BrushColourPropetyChanged);
            }
        }

        private static readonly PropertyChangedEventArgs XLocationPropertyChanged = new PropertyChangedEventArgs("XLocation");
        private double _xLocation;
        public double XLocation
        {
            get { return _xLocation; }
            set
            {
                if (_xLocation == value)
                    return;

                _xLocation = value;
                RaisePropertyChanged(XLocationPropertyChanged);
            }
        }

        private static readonly PropertyChangedEventArgs YLocationPropertyChanged = new PropertyChangedEventArgs("YLocation");
        private double _yLocation;
        public double YLocation
        {
            get { return _yLocation; }
            set
            {
                if (_yLocation == value)
                    return;

                _yLocation = value;
                RaisePropertyChanged(YLocationPropertyChanged);
            }
        }

        public OrganismDrawModel(IOrganism organism, UIConainerSize uiConainerSize)
        {
            Organism = organism;
            Organism.RegistrationChangeEvent += SetBrushColour;

            _xViewBoundry = SimEnv.Instance.HostEnv.EnvironmentScenario.BoundryXRadiusMax;
            if (_xViewBoundry == 0)
                _xViewBoundry = 3;

            _yViewBoundry = SimEnv.Instance.HostEnv.EnvironmentScenario.BoundryZRadiusMax;
            if (_yViewBoundry == 0)
                _yViewBoundry = 3;

            _uiContainer = uiConainerSize;

            organism.LocationChangeEvent += UpdateOrganismLocation;

            UpdateOrganismLocation(organism.Location);

            SetBrushColour();
        }

        private void SetBrushColour()
        {
            BrushColour = Organism.Born ? Brushes.Black : Brushes.Transparent;
        }

        private void UpdateOrganismLocation(ILocation location)
        {
            XLocation = (_uiContainer.ContainerWidth - (_uiContainer.ContainerWidth / EdgeSpace)) / 2
                + (location.XAxis * (_uiContainer.ContainerWidth - (_uiContainer.ContainerWidth / EdgeSpace)) / 2 / (_xViewBoundry));

            YLocation = (_uiContainer.ContainerHeight - (_uiContainer.ContainerHeight / EdgeSpace)) / 2
                + (location.ZAxis * (_uiContainer.ContainerHeight - (_uiContainer.ContainerHeight / EdgeSpace)) / 2 / (_yViewBoundry));
        }

        public void Dispose()
        {
            Organism.LocationChangeEvent -= UpdateOrganismLocation;
            Organism.RegistrationChangeEvent -= SetBrushColour;
        }
    }
}